package entity;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by sqy on 2018/11/21.
 */
public class rangerAudit {

    protected static String FIELD_SEPARATOR = ";";

    @Setter
    @Getter
    protected String id = null;

    @Setter
    @Getter
    protected int repoType = 0;

    @Setter
    @Getter
    protected String repo = null;

    @Setter
    @Getter
    protected String reqUser = null;

    @Setter
    @Getter
    protected Date evtTime = new Date();

    @Setter
    @Getter
    protected String access = null;

    @Setter
    @Getter
    protected String resource = null;

    @Setter
    @Getter
    protected String resType = null;

    @Setter
    @Getter
    protected String action = null;

    @Setter
    @Getter
    protected short result = 0; // 0 - DENIED; 1 - ALLOWED; HTTP return

    @Setter
    @Getter
    protected long policy = 0;

    @Setter
    @Getter
    protected String reason = null;

    @Setter
    @Getter
    protected String enforcer = null;

    @Setter
    @Getter
    protected String sess = null;

    @Setter
    @Getter
    protected String cliType = null;

    @Setter
    @Getter
    protected String cliIP = null;

    @Setter
    @Getter
    protected String reqData = null;

    @Setter
    @Getter
    protected String logType = null;

    @Setter
    @Getter
    protected String _ttl_ = null;

    @Setter
    @Getter
    protected String _expire_at_ = null;

    @Setter
    @Getter
    protected long _version_ = 0;

    /**
     * This to ensure order within a session. Order not guaranteed across
     * processes and hosts
     */

    @Setter
    @Getter
    protected long seq_num = 0;

    @Setter
    @Getter
    protected long event_count = 1;

    @Setter
    @Getter
    protected long event_dur_ms = 0;

    @Setter
    @Getter
    protected Set<String> tags = new HashSet<String>();

    public rangerAudit(){}

    public rangerAudit(String id,String access,String enforcer,String repo,String reqUser,
                       String resource,String cliIP,String logType,short result,long policy,int repoType,
                       String resType,String reason,String action,Date evtTime,long seq_num,long event_count,
                       long event_dur_ms,String _ttl_,String _expire_at_,long _version_){

        this.id = id;
        this.access = access;
        this.enforcer = enforcer;
        this.repo = repo;
        this.reqUser = reqUser;
        this.resource = resource;
        this.cliIP = cliIP;
        this.logType = logType;
        this.result = result;
        this.policy = policy;
        this.repoType = repoType;
        this.resType = resType;
        this.reason = reason;
        this.action = action;
        this.evtTime = evtTime;
        this.seq_num = seq_num;
        this.event_count = event_count;
        this.event_dur_ms = event_dur_ms;
        this._ttl_ = _ttl_;
        this._expire_at_ =_expire_at_;
        this._version_ = _version_;
    }


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("rangerAudit{");
        toString(sb);
        sb.append("}");

        return sb.toString();
    }

    protected StringBuilder toString(StringBuilder sb) {
        sb.append("id=").append(id)
                .append(FIELD_SEPARATOR).append("access=")
                .append(access).append(FIELD_SEPARATOR).append("enforcer=")
                .append(enforcer).append(FIELD_SEPARATOR).append("repo=")
                .append(repo).append(FIELD_SEPARATOR)
                .append("sess=").append(sess)
                .append(FIELD_SEPARATOR).append("reqUser=")
                .append(reqUser).append(FIELD_SEPARATOR)
                .append("reqData=").append(reqData)
                .append(FIELD_SEPARATOR).append("resource=").append(resource)
                .append(FIELD_SEPARATOR).append("cliIP=")
                .append(cliIP).append(FIELD_SEPARATOR)
                .append("logType=").append(logType).append(FIELD_SEPARATOR)
                .append("result=").append(result).append(FIELD_SEPARATOR)
                .append("policy=").append(policy)
                .append(FIELD_SEPARATOR).append("resType=")
                .append(resType).append(FIELD_SEPARATOR)
                .append("action=").append(action).append(FIELD_SEPARATOR)
                .append("evtTime=").append(evtTime)
                .append(FIELD_SEPARATOR).append("seq_num=").append(seq_num)
                .append(FIELD_SEPARATOR).append("event_count=")
                .append(event_count).append(FIELD_SEPARATOR)
                .append("event_dur_ms=").append(event_dur_ms)
                .append("_ttl_=").append(_ttl_)
                .append(FIELD_SEPARATOR).append("_expire_at_=").append(_expire_at_)
                .append(FIELD_SEPARATOR).append("_version_=").append(_version_)
                .append(FIELD_SEPARATOR).append("seq_num=").append(seq_num)
                .append(FIELD_SEPARATOR).append("event_count=")
                .append(event_count).append(FIELD_SEPARATOR)
                .append("tags=").append("[")
                .append(StringUtils.join(tags, ", "))
                .append("]");

        return sb;
    }

}
